176B - Word Cut - CodeForces Solution


dp *1700

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map> 
#include<set>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

const int mod = 1e9 + 7;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	
	string st, ed;
	cin >> st >> ed;
	int fg = 0;
	if(st != ed) fg = 1;
	st = ' ' + st + st;
	ed = ' ' + ed;
	int k;
	cin >> k;
	vector<int> ne(ed.size());
	for(int i = 2, j = 0; i < ed.size(); i ++ ) {
	    while(j && ed[i] != ed[j + 1]) j = ne[j];
	    if(ed[i] == ed[j + 1]) j ++ ;
	    ne[i] = j;
	}
	int cnt = 0;
	for(int i = 1, j = 0; i < st.size() - 1; i ++ ) {
	    while(j && st[i] != ed[j + 1]) j = ne[j];
	    if(st[i] == ed[j + 1]) j ++ ;
	    if(j == ed.size() - 1) cnt ++ ;
	}
	vector<vector<int> > dp(k + 1, vector<int>(2, 0));
	dp[0][fg] = 1; // 0:代表ed  1:代表非ed的串
	for(int i = 1; i <= k; i ++ ) {
	    dp[i][0] = ((ll)dp[i - 1][0] * (cnt - 1) + (ll)dp[i - 1][1] * cnt) % mod;
	    dp[i][1] = ((ll)dp[i - 1][0] * (ed.size() - 1 - cnt) + (ll)dp[i - 1][1] * (ed.size() - 1 - cnt - 1)) % mod;
	}
	cout << dp[k][0];
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck